/**
 * 产品分类列表
 * 文件命名格式：zmn.模块名称.表名称.功能.jas
 * @author heguanghua
 * @since 2018/10/18
 */
layui.define(['index', 'form', 'table', 'laypage', 'layedit', 'laydate'], function (exports) {
    var $ = layui.$;
    var form = layui.form;
    var layer = layui.layer;
    var table = layui.table;
    var laypage = layui.laypage;
    let laydate = layui.laydate;
    table.init('zmn-table', {
        toolbar: "#zmn-table-tool",
        page: true,
        limit: json.pageSize,
        height: 'full-140',
        defaultToolbar: ['filter']
    });
    laypage.render(zmnPage.laypageOptions(json));

    // 渲染日期范围选择器
    laydate.render({
        elem: '#timeRange',
        range: true,
        format: 'yyyy/MM/dd',
    });

    // 区域下拉框联动
    laySelectLinkage(form, "/common/area/listChilds.action", "provinceId", "cityId", "countyId");

    var ids = new Array();
    var serviceCompanyIds = new Array();
    var noPlatCompanyIds = new Array();

    // 选择（全选或单选）
    table.on("checkbox(zmn-table)", function () {
        serviceCompanyIds = new Array();
        noPlatCompanyIds = new Array();
        ids = table.checkStatus('zmn-table').data.map((item) => {
            if(item.type == 130){
                serviceCompanyIds.push(item.companyId);
            }
            if(item.type != 100){
                noPlatCompanyIds.push(item.companyId);
            }
            return item.companyId;
        });
    });

    // 启用/停用
    table.on("toolbar(zmn-table)", function (obj) {
        if (obj.event != "start" && obj.event != "stop") {
            return;
        }
        if (ids.length === 0) {
            return layer.alert("请选择至少一条数据！");
        }
        if(serviceCompanyIds.length>0){
            return layer.alert("服务商类型的公司不能变更状态！");
        }
        zmnPage.layStartStop(obj, "/company/updateStatus.action", ids.join(","));
    });

    // 修改排序
    table.on("edit(zmn-table)", function (obj) {
        if(obj.data.type==130){
            return layer.alert("服务商类型的公司不能修改排序！");
        }
        zmnPage.laySort(obj, "/company/updateSort.action", obj.data.companyId);
    });

    //查询条件重置
    form.on('submit(zmn-company-form-reset)', function (obj) {
        form.val("zmn-company-filter", {
            "nameOrCompanyId": "" // "name": "value"
            ,"contactOrPhone": ""
            ,"type": ''
            ,"plat": ""
            ,"provinceId": ""
            ,"cityId": ""
            ,"countyId": ""
            ,"status": ""
            ,"timeRange": ""
        });
        return false;
    });

    // 重建公司索引
    $(document).on('click', '#rebuildCompanyIndex ', function () {
        let loadingIndex;
        $.ajax({
            type: "POST",
            url: "/company/rebuildCompanyIndex.action",
            headers: {
                Accept: "application/json"
            },
            contentType: "application/json",
            dataType: "json",
            beforeSend: function () {
                loadingIndex = layer.load(1, {
                    shade: [0.5, '#DBDBDB']
                });
            },
            success: function (json) {
                if (json.status==200) {
                    layer.msg(json.message, {"icon": 1});
                } else {
                    layer.msg(json.message, {"icon": 2});
                }
                layer.close(loadingIndex);
            }
        });
    });

    // 注册开票
    $(document).on('click', '#registerInvoicing ', function () {
        if (ids.length === 0) {
            return layer.alert("请勾选需注册的公司！");
        }
        if(noPlatCompanyIds.length>0){
            return layer.alert("非平台公司类型，不允许注册！");
        }
        zmnPage.ajaxJson("/company/registerInvoicing.action", {ids: ids.join(",")});
    });

    function laySelectLinkage(form, url, selectOneId, selectTwoId, selectThrId) {
        var $ = layui.$;
        form.on("select(" + selectOneId + ")", function (obj) {
            $("#" + selectTwoId).html( '<option value="">选择</option>');
            $("#" + selectThrId).html( '<option value="">选择</option>');
            form.render("select");
            if (zmnUtil.isNull(obj) || obj.value == 0) {
                return;
            }
            $.ajax({
                type: "GET", url: url,
                dataType: "json", data: {parentId: obj.value},
                success: function (items) {
                    var optionHtml = '<option value="">选择</option>';
                    for (var i = 0; i < items.length; i++) {
                        optionHtml += '<option value="' + items[i][0] + '">' + items[i][1] + '</option>';
                    }
                    $("#" + selectTwoId).html(optionHtml);
                    form.render("select");
                }
            });
        });

        if (zmnUtil.isEmptyNull(selectThrId)) {
            return;
        }
        ;

        form.on("select(" + selectTwoId + ")", function (obj) {
            $("#" + selectThrId).html( '<option value="">选择</option>');
            form.render("select");
            if (zmnUtil.isNull(obj) || obj.value == 0) {
                return ;
            }
            $.ajax({
                type: "GET", url: url,
                dataType: "json", data: {parentId: obj.value},
                success: function (items) {
                    var optionHtml = '<option value="">选择</option>';
                    for (var i = 0; i < items.length; i++) {
                        optionHtml += '<option value="' + items[i][0] + '">' + items[i][1] + '</option>';
                    }
                    $("#" + selectThrId).html(optionHtml);
                    form.render("select");
                }
            });
        });
    }

    //对外暴露的接口
    exports('art_company_list', {});
});